package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.sys.entity.Tenant;
import com.dataseek.iot.sys.mapper.TenantMapper;
import com.dataseek.iot.sys.service.TenantService;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.sys.vo.TenantVo;
import com.dataseek.iot.toolikit.common.util.EncodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class TenantServiceImpl implements TenantService {


    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public TenantVo queryTenant(String companyCode, String companyName, String status, Integer num, Integer size) {

        TenantVo tenantVo = new TenantVo();
        QueryWrapper<Tenant> qw = new QueryWrapper<>();
        if (companyName != null && !companyName.isEmpty()) {
            qw.like("company_name", companyName);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq("status", status);
        }
        if (companyCode != null && !companyCode.isEmpty()) {
            qw.eq("company_code", companyCode);
        }
        IPage<Tenant> page = new Page<>(num, size);
        List<Tenant> mapList = tenantMapper.selectPage(page, qw).getRecords();
        tenantVo.setPage(num);
        tenantVo.setSize(size);
        tenantVo.setTotal(page.getTotal());
        tenantVo.setList(mapList);
        return tenantVo;
    }

    @Override
    public Integer updateTenant(Tenant tenant) {
        Tenant tenant1 = new Tenant();
        tenant1.setTenantId(tenant.getTenantId());
        tenant1.setTenantName(tenant.getTenantName());
        tenant1.setTelephone(tenant.getTelephone());
        tenant1.setCompanyAddress(tenant.getCompanyAddress());
        int resultCode = tenantMapper.updateById(tenant1);
        return resultCode;
    }

    @Override
    public Integer insertTenant(Tenant tenant) {
        int resultCode = 0;
        String code = tenant.getCompanyCode();
        QueryWrapper<Tenant> qw = new QueryWrapper<>();
        qw.eq("company_code", code);
        Integer count = tenantMapper.selectCount(qw);
        if (count ==0 ) {
            String pw = tenant.getTenantPassword();
            String pwEnCode = EncodeUtil.passwordEncoder(pw);
            tenant.setTenantPassword(pwEnCode);
            tenant.setSuperAdminName("postgres");
            tenant.setSuperAdminPassword("Dataseek2022!@#$");
            tenant.setDatabaseName(code);
            // 更新租户信息不涉及密码
            // TODO: 2022/3/19 密码修改和重置需要单独接口
            tenant.setDbHost("172.26.153.15");
            tenant.setDbPort(5432);
            tenant.setDbType("postgresql");
            tenant.setSchemaName("tenant");
            tenant.setDriverName("org.postgresql.Driver");
            log.info("tenant:" + tenant);
            resultCode = tenantMapper.insert(tenant);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;
    }

    @Override
    public Integer deleteTenant(List<Integer> tenantIds){
        int resultCode = tenantMapper.deleteBatchIds(tenantIds);
        return resultCode;
    }

}
